package com.dff.jrfw;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.dff.jrfw.databinding.ActivityForgotPsdOneBinding;
import com.dff.jrfw.remote.CustomCallBack;
import com.dff.jrfw.remote.RetrofitUtils;
import com.dff.jrfw.utils.InputStrCheckUtil;
import com.dff.jrfw.utils.ToastUtil;
import com.dff.jrfw.utils.Util;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class ForgotPsdOneActivity extends BaseActivity implements View.OnClickListener {

    private ActivityForgotPsdOneBinding binding;
    private String username;

    public static void callMe(Context context, String username) {
        Intent intent = new Intent(context, ForgotPsdOneActivity.class);
        intent.putExtra("username", username);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_forgot_psd_one);
        username = getIntent().getStringExtra("username");
        initView();
    }

    private void initView() {
        binding.ivBack.setOnClickListener(this);
        binding.btnNext.setOnClickListener(this);
        binding.ivInputClearMobile.setOnClickListener(this);
        binding.etPhone.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                String etPayPsd = charSequence.toString().trim();
                if (etPayPsd.length() == 0) {
                    binding.ivInputClearMobile.setVisibility(View.INVISIBLE);
                } else {
                    binding.ivInputClearMobile.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_next:
                sendSmsCode();
                Util.hideKeyBoard(binding.btnNext);
                break;
            case R.id.iv_input_clear_mobile:
                binding.etPhone.setText("");
                break;
        }
    }


    private void sendSmsCode() {
        String etPhone = binding.etPhone.getText().toString().trim();
        if (!InputStrCheckUtil.isMobileNO(etPhone)) {
            ToastUtil.toast("请输入正确的手机号");
            return;
        }
        showLoading();
        RetrofitUtils.getService().sendSmsCode("1", "1", etPhone,"2").enqueue(new CustomCallBack<ResponseBody>() {
            @Override
            public void onFailure(Call call, Throwable t) {
                super.onFailure(call, t);
                hideLoading();
            }

            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                hideLoading();
                try {
                    if (response.body() != null) {
                        String result = response.body().string();
                        JSONObject jsonObject = new JSONObject(result);
                        int code = jsonObject.getInt("code");
                        if (code == 200) {
                            ToastUtil.toast("验证码已发送");
                            ForgotPsdTwoActivity.callMe(ForgotPsdOneActivity.this, etPhone, username);
                        } else {
                            String msg = jsonObject.getString("msg");
                            ToastUtil.toast(msg);
                        }
                    } else {
                        ResponseBody errorResponseBody = response.errorBody();
                        if (errorResponseBody != null) {
                            ToastUtil.toast(errorResponseBody.string());
                        }
                    }
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
            }
        });
    }


}